package colopl.http.client;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import colopl.http.util.MessageResource;
import gae.util.http.client.Cookie;
import gae.util.http.client.Header;
import gae.util.http.client.HttpRequest;
import gae.util.http.client.Parameter;

/**
 * @author osora
 */
public class ColoplHttpRequestFactory {
    
    private static final Log logger = LogFactory.getLog(ColoplHttpRequestFactory.class);
    
    protected final static String PC_TOP_URL = "http://pl.colopl.jp/c/c/pc";
    
    protected final static String PC_AUTH_URL = "http://colopl.jp/s/c/authentication/entrance";

    protected final static String ITEM_WH_URL = "http://colopl.jp/s/itemWH.do";
    
    protected static final String LINK_URL = "http://colopl.jp/s/link.do";
    
    protected final static String PRESENT_OMIYAGE_URL = "http://colopl.jp/s/presentOmiyage.do";
    
    protected final static String PRESENT_OMIYAGE_DO_URL = "http://colopl.jp/s/presentOmiyageDo.do";
    
    protected final static String BUILD_BATCH_DO_URL = "http://colopl.jp/s/buildBatchDo.do";
    
    protected final static String ITEM_SET_WHACTION_DO = "http://colopl.jp/s/itemSetWHAction.do";
    
    
    
    /**
     * PC TOP画面用のリクエストを作成する
     * @return
     */
    public HttpRequest createPcTop() {
        HttpRequest req = new HttpRequest("GET", PC_TOP_URL);
        req.setHeader(ColoplHttpRequestUtil.getDefaultRequestHeader());
        
        return req;
    }
    
    /**
     * ログイン情報を送信するリクエストを作成する
     * @param user
     * @param value
     * @param hdb
     * @return
     */
    public HttpRequest createPcTopLogin(ColoplUser user, String value, String hdb) {
        
        Parameter param = new Parameter();
        param.set("username", user.getUsername());
        param.set("password", user.getPassword());
        param.set("submit", value);
        param.set("hdb", hdb);
        param.set("submit.x", "27");
        param.set("submit.y", "21");
        
        Cookie reqCookie = new Cookie();
        reqCookie.addParam("JSESSIONID", user.getJsessionid(), 0);
        Header reqHeader = ColoplHttpRequestUtil.getDefaultRequestHeader();
        reqHeader.setCookie(reqCookie);
        
        HttpRequest req = new HttpRequest("POST", PC_TOP_URL);
        req.setParameter(param);
        req.setHeader(reqHeader);
        
        return req;
    }

    /**
     * 認証するリクエストを作成する
     * @param user
     * @return
     */
    public HttpRequest createAuthenticationEntrance(ColoplUser user) {
        HttpRequest req = new HttpRequest("GET", PC_AUTH_URL);
        Header reqHeader = ColoplHttpRequestUtil.getDefaultRequestHeader();
        reqHeader.setCookie(ColoplHttpRequestUtil.getDefaultRequestCookie(user));
        req.setHeader(reqHeader);
        
        return req;
    }
    
    /**
     * コロプラのトップ画面（ゲームのTOP画面）のリクエストを作成する
     * @param user
     * @param location
     * @return
     */
    public HttpRequest createLocation(ColoplUser user, String location) {
        
        HttpRequest req = new HttpRequest("GET", location);
        Header reqHeader = ColoplHttpRequestUtil.getDefaultRequestHeader();
        reqHeader.add("Referer", PC_TOP_URL);
        reqHeader.setCookie(ColoplHttpRequestUtil.getDefaultRequestCookie(user));
        req.setHeader(reqHeader);
        
        return req;
    }
    
    /**
     * 倉庫ページのリクエストを作成する
     * @param user
     * @param page
     * @return
     */
    public HttpRequest createWHItem(ColoplUser user, int page) {
        
        if (page < 0) {
            throw new IllegalArgumentException(MessageResource.getMessage("CHC-001", page));
        }
        
        HttpRequest req = new HttpRequest("GET", ITEM_WH_URL);
        Header reqHeader = ColoplHttpRequestUtil.getDefaultRequestHeader();
        reqHeader.setCookie(ColoplHttpRequestUtil.getDefaultRequestCookie(user));
        req.setHeader(reqHeader);
        
        Parameter reqParam = ColoplHttpRequestUtil.getDefaultRequestParameter(user); 
        reqParam.set("point", String.valueOf(page));
        req.setParameter(reqParam);
        
        if (logger.isDebugEnabled()) {
            logger.debug("Created WHItem request. req=" + req);
        }
        
        return req;
    }
    
    /**
     * リンクコロニーページのリクエストを作成する
     * @param user
     * @param section
     * @param groupId
     * @return
     */
    public HttpRequest createLinkGroup(ColoplUser user, String section, int groupId) {
        
        if (groupId < -2 || groupId > 9) {
            throw new IllegalArgumentException(MessageResource.getMessage("CHC-002", groupId));
        }
        if (!"all".equals(section) && !"near".equals(section) && !"far".equals(section) && !"friend".equals(section)) {
            throw new IllegalArgumentException(MessageResource.getMessage("CHC-003", section));
        }
        
        HttpRequest req = new HttpRequest("POST", LINK_URL);
        Header reqHeader = ColoplHttpRequestUtil.getDefaultRequestHeader();
        reqHeader.setCookie(ColoplHttpRequestUtil.getDefaultRequestCookie(user));
        req.setHeader(reqHeader);
        
        Parameter reqParam = ColoplHttpRequestUtil.getDefaultRequestParameter(user); 
        reqParam.set("section", section);
        reqParam.set("groupId", String.valueOf(groupId));
        req.setParameter(reqParam);
        
        if (logger.isDebugEnabled()) {
            logger.debug("Created LingGroup request. req=" + req);
        }
        
        return req;
    }
    
    /**
     * プレゼント用のお土産選択画面のリクエストを作成する
     * @param user
     * @param toID
     * @param area
     * @return
     */
    public HttpRequest createPresentOmiyage(ColoplUser user, int toID, int area) {
        
        if (area < 0 || area > 11) {
            throw new IllegalArgumentException(MessageResource.getMessage("CHC-004", area));
        }
        
        HttpRequest req = new HttpRequest("GET", PRESENT_OMIYAGE_URL);
        Header reqHeader = ColoplHttpRequestUtil.getDefaultRequestHeader();
        reqHeader.setCookie(ColoplHttpRequestUtil.getDefaultRequestCookie(user));
        req.setHeader(reqHeader);
        
        Parameter reqParam = ColoplHttpRequestUtil.getDefaultRequestParameter(user); 
        reqParam.set("toID", String.valueOf(toID));
        reqParam.set("area", String.valueOf(area));
        req.setParameter(reqParam);
        
        if (logger.isDebugEnabled()) {
            logger.debug("Created PresentOmiyage request. req=" + req);
        }
        
        return req;
    }
    
    /**
     * お土産のプレゼントリクエストを作成する
     * @param user
     * @param toID
     * @param area
     * @param omiyageID
     * @param num
     * @return
     */
    public HttpRequest createPresentOmiyageDo(ColoplUser user, int toID, int area, int omiyageID, int num) {
        
        if (area < 0 || area > 11) {
            throw new IllegalArgumentException(MessageResource.getMessage("CHC-005", area));
        }
        if (num < 1) {
            throw new IllegalArgumentException(MessageResource.getMessage("CHC-006", num));
        }
        
        HttpRequest req = new HttpRequest("POST", PRESENT_OMIYAGE_DO_URL);
        Header reqHeader = ColoplHttpRequestUtil.getDefaultRequestHeader();
        reqHeader.setCookie(ColoplHttpRequestUtil.getDefaultRequestCookie(user));
        req.setHeader(reqHeader);
        
        Parameter reqParam = ColoplHttpRequestUtil.getDefaultRequestParameter(user); 
        reqParam.set("toID", String.valueOf(toID));
        reqParam.set("area", String.valueOf(area));
        reqParam.set("omiyageID", String.valueOf(omiyageID));
        reqParam.set("num", String.valueOf(num));
        req.setParameter(reqParam);
        
        if (logger.isDebugEnabled()) {
            logger.debug("Created PresentOmiyageDo request. req=" + req);
        }
        
        return req;
    }
    
    /**
     * 上級者一括建造用のリクエストを作成する
     * @param user
     * @param serial
     * @param x
     * @param y
     * @param x2
     * @param y2
     * @param action
     * @return
     */
    public HttpRequest createBuildBatchDo(ColoplUser user, 
            int serial, int x, int y, int x2, int y2, int action) {
        
        if (x < 0 || y < 0 || x2 < 0 || y2 < 0 ||
                x > 9 || y > 9 || x2 > 9 || y2 > 9) {
            throw new IllegalArgumentException(MessageResource.getMessage("CHC-007", x, y, x2, y2));
        }
        if (serial < 0 || serial > 9) {
            throw new IllegalArgumentException(MessageResource.getMessage("CHC-008", serial));
        }
        if (action < 0 || action > 7) {
            throw new IllegalArgumentException(MessageResource.getMessage("CHC-009", action));
        }
        
        HttpRequest req = new HttpRequest("POST", BUILD_BATCH_DO_URL);
        Header reqHeader = ColoplHttpRequestUtil.getDefaultRequestHeader();
        reqHeader.setCookie(ColoplHttpRequestUtil.getDefaultRequestCookie(user));
        req.setHeader(reqHeader);
        
        Parameter reqParam = ColoplHttpRequestUtil.getDefaultRequestParameter(user); 
        reqParam.set("x", String.valueOf(x));
        reqParam.set("y", String.valueOf(y));
        reqParam.set("x2", String.valueOf(x2));
        reqParam.set("y2", String.valueOf(y2));
        reqParam.set("serial", String.valueOf(serial));
        reqParam.set("action", String.valueOf(action));
        req.setParameter(reqParam);
        
        if (logger.isDebugEnabled()) {
            logger.debug("Created BuildBatchDo request. req=" + req);
        }
        
        return req;
    }
    
    public HttpRequest createItemSetWHAction(ColoplUser user, int autoID, int priority, String msg, int price) {
        
        if (priority < 0 ) {
            throw new IllegalArgumentException(MessageResource.getMessage("CHC-010", priority));
        }
        if (price < 0) {
            throw new IllegalArgumentException(MessageResource.getMessage("CHC-011", price));
        }
        
        if (msg == null) {
            msg = "";
        }
        
        HttpRequest req = new HttpRequest("POST", ITEM_SET_WHACTION_DO);
        Header reqHeader = ColoplHttpRequestUtil.getDefaultRequestHeader();
        reqHeader.setCookie(ColoplHttpRequestUtil.getDefaultRequestCookie(user));
        req.setHeader(reqHeader);
        
        Parameter reqParam = ColoplHttpRequestUtil.getDefaultRequestParameter(user); 
        reqParam.set("autoID", String.valueOf(autoID));
        reqParam.set("priority", String.valueOf(priority));
        reqParam.set("msg", msg);
        reqParam.set("price", String.valueOf(price));
        req.setParameter(reqParam);
        
        if (logger.isDebugEnabled()) {
            logger.debug("Created ItemSetWHAction request. req=" + req);
        }
        
        return req;
    }
}
